package com.j283_5.wisdom_park.controller.car_management;


import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.j283_5.wisdom_park.common.result.ResultData;
import com.j283_5.wisdom_park.pojo.CarRecord;
import com.j283_5.wisdom_park.pojo.Visitors;
import com.j283_5.wisdom_park.service.CarRecordService;
import com.j283_5.wisdom_park.vo.car_management.CarRecordVo;
import com.j283_5.wisdom_park.vo.car_management.SelectCarRecordVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.ibatis.annotations.Param;
import org.apache.poi.hssf.usermodel.HSSFCellStyle;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.Font;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.OutputStream;
import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.Iterator;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author 蒋翔
 * @since 2022-04-28
 */
@RestController
@RequestMapping("/car-record")
@Api(value = "车辆进出管理",tags = "车辆进出管理",produces="蒋翔")
public class CarRecordController {
    @Resource
    private CarRecordService carRecordService;

    @RequestMapping(value = "/carRecordVoList" , method = RequestMethod.GET)
    @ApiOperation(value = "查询所有车辆信息",notes = "查询所有车辆信息（分页）",produces="蒋翔", response = ResultData.class)
    public ResultData carRecordVoList(@Param("page") Page<CarRecord> page, @Param("selectCarRecordVo") SelectCarRecordVo selectCarRecordVo){
//        CarRecordVo carRecordVo1 = new CarRecordVo(1,"豫A00FX2", "2011款 2.8L 无级 两驱 舒适版", "张旭", "18767256412",
//                "包月车", "出场", new Timestamp(System.currentTimeMillis()));
//        CarRecordVo carRecordVo2 = new CarRecordVo(2,"浙A88888", "2011款 3.0T 手自一体 Quattro 四驱", "李君虞", "13208432854",
//                "临时车", "如场", new Timestamp(System.currentTimeMillis()));
//        ArrayList<CarRecordVo> recordVoArrayList = new ArrayList<>();
//        recordVoArrayList.add(carRecordVo1);
//        recordVoArrayList.add(carRecordVo2);
//        return ResultData.ok().data(recordVoArrayList);
        return carRecordService.carRecordVoList(page,selectCarRecordVo);
    }

    @RequestMapping(value = "/deleteRecordVo" , method = RequestMethod.POST)
    @ApiOperation(value = "删除车辆进出记录",notes = "删除车辆进出记录（可批量）",produces="蒋翔", response = ResultData.class)
    public ResultData deleteRecordVo(@RequestBody ArrayList<Integer> crecIdList){
//        return ResultData.ok().message("删除成功!");
        return carRecordService.deleteRecordVo(crecIdList);
    }


    @RequestMapping(value = "/downCarRecord" , method = RequestMethod.GET)
    @ApiOperation(value = "导出车辆进出信息",notes = "导出车辆进出信息",produces="蒋翔", response = ResultData.class)
    public void downCarRecord(HttpServletRequest request, HttpServletResponse response) throws IOException {
//        return ResultData.ok().message("删除成功！");
        //生成Excel
        //创建HSSFWorkbook对象(excel的文档对象)
        HSSFWorkbook wkb = new HSSFWorkbook();
        //建立新的sheet对象（excel的表单）
        HSSFSheet sheet = wkb.createSheet("车辆进出信息表");//sheet1
        HSSFRow row2 = sheet.createRow(0);
        //创建一个新字体并对其进行更改。
        Font font = wkb.createFont();
        font.setFontHeightInPoints((short) 12);
        font.setFontName("宋体");//simhei
        font.setBold(true);
        /*        font.setItalic(true);*/
        /*        font.setStrikeout(true);*/
        //字体设置为一种样式，因此请创建一种新样式来使用。
        HSSFCellStyle hssfCellStyle = wkb.createCellStyle();
        hssfCellStyle.setFont(font);
        //创建单元格并设置单元格内容
        row2.createCell(0).setCellValue("车辆号码");
        row2.createCell(1).setCellValue("车辆型号");
        row2.createCell(2).setCellValue("车主姓名");
        row2.createCell(3).setCellValue("联系方式");
        row2.createCell(4).setCellValue("车辆类型");
        row2.createCell(5).setCellValue("进出方向");
        row2.createCell(6).setCellValue("进出时间");
        //循环设置所有列数的样式
        Iterator<Cell> it = row2.iterator();
        while (it.hasNext()) {
            it.next().setCellStyle(hssfCellStyle);
        }
        //适合的列宽
        sheet.autoSizeColumn(1);
        //输出Excel文件
        OutputStream output = response.getOutputStream();
        response.reset();
        response.setCharacterEncoding("utf-8");
        response.setHeader("Content-disposition", "attachment; filename=" + "carRecord" + (System.currentTimeMillis() % 10) + ".xls");
        //导出的文件格式
        response.setContentType("application/msexcel");
        wkb.write(output);
        output.close();
    }



}

